import javax.management.openmbean.InvalidOpenTypeException;

//此处不考虑泛型参数，只是基于 String 进行存储
class MyBlockingQueue {
    private String[] data = null;
    private int head = 0;
    private int tail = 0;
    private int size = 0;
    private Object locker = new Object();

    public MyBlockingQueue(int capacity) {
        data = new String[capacity];
    }

    public void put(String s) throws InterruptedException {
        //加锁的对象，可以单独定义一个，也可以直接就地使用this
        synchronized (locker) {
            while (size == data.length) {
                //队列满了，需要阻塞
                //return;
                locker.wait();
            }
            data[tail] = s;
            tail++;
            if (tail >= data.length) {
                tail = 0;
            }
            size++;
            //唤醒 take 的阻塞
            locker.notify();
        }
    }

    public String take() throws InterruptedException {
        String ret = "";
        synchronized (locker) {
            while (size == 0) {
                //队列为空,需要阻塞
                //return null;
                locker.wait();
            }
            ret = data[head];
            head++;
            if (head >= data.length) {
                head = 0;
            }
            size--;

            //唤醒 put 的阻塞
            locker.notify();
        }
        return ret;
    }
}


public class Demo12 {
    public static void main(String[] args) {

    }
}
